/*
 * Mutex.cpp
 *
 */

#include "Mutex.h"

//
// Mutex
//

Mutex::Mutex() {
	pthread_mutex_init(&mutex, NULL);
}

Mutex::~Mutex() {
	pthread_mutex_destroy(&mutex);
}

void Mutex::lock() {
	pthread_mutex_lock(&mutex);
}

void Mutex::unlock() {
	pthread_mutex_unlock(&mutex);
}

//
// Condition Variable
//

ConditionVariable::ConditionVariable() {
	pthread_cond_init(&cv, NULL);
}

ConditionVariable::~ConditionVariable() {
	pthread_cond_destroy(&cv);
}

void ConditionVariable::broadcast() {
	pthread_cond_broadcast(&cv);
}

void ConditionVariable::signal() {
	pthread_cond_signal(&cv);
}

void ConditionVariable::wait() {
	pthread_cond_wait(&cv, &mutex);
}

//
// Lock
//

Lock::Lock(Mutex& mutex) : mutex(mutex) {
	mutex.lock();
}

Lock::~Lock() {
	mutex.unlock();
}
